var express = require('express')

var bodyParser = require('body-parser')

var comments = [
	{"name": "张三", "message": "卡卡卡的可靠的", "dateTime": "2018-1-29 12:15:30"},
	{"name": "李四", "message": "大量订单", "dateTime": "2018-2-29 15:25:36"}
]

var app = express()

// parse application/x-www-form-urlencoded
app.use(bodyParser.urlencoded({ extended: false }))

// parse application/json
app.use(bodyParser.json())

app.use('/public/', express.static('./public/'))

app.engine('html', require('express-art-template'))

app.get('/', function(req, res){
	res.render('index.html', {
		comments: comments
	})
})

app.get('/post', function(req, res){
	res.render('post.html')
})

app.post('/post', function(req, res){
	var comment = req.body
	comment.dateTime = new Date().toLocaleString()
	comments.unshift(comment)
	// res.statusCode = 302
	// res.setHeader('Location', '/')
	// res.end()
	res.redirect('/')
})

app.listen(5000, function(){
	console.log('server running 5000...')
})